import os
import numpy as np

import py_indoor_context
import training_helpers
import training_params

np.set_printoptions(precision=3)

feature_store = os.path.join(training_params.FeatureStoreBase, 'manual')

test_set = training_params.Datasets.Large.TestSet
#test_set = [('lab_kitchen1', [25])]

# Iteration 275, depth loss, mono-all features, large dataset
params275 = py_indoor_context.ManhattanHyperParameters(
  np.array([-0.011, -0.08 ,  0.131, -0.051,  0.114, -0.089,  0.031,  0.006, -0.053,  0.003,
   -0.004,  0.001, -0.048,  0.048,  0.   ,  0.283, -0.126, -0.226, -0.069,  0.046,
    0.037, -0.032,  0.021,  0.016, -0.033,  0.017,  0.023, -0.093,  0.015,  0.112,
   -0.042,  0.006,  0.052, -0.024,  0.014,  0.016,  0.027, -0.02 , -0.012,  0.001,
    0.009, -0.016,  0.033,  0.02 , -0.076,  0.019, -0.023,  0.006,  0.003, -0.008,
    0.007,  0.005,  0.001, -0.009, -0.021,  0.016,  0.009,  0.022, -0.057,  0.057,
    0.021, -0.022,  0.001,  0.01 , -0.117,  0.155, -0.06 ,  0.133, -0.105, -0.023,
    0.056, -0.054,  0.008,  0.023, -0.051,  0.014, -0.003, -0.019,  0.056,  0.06,
   -0.099, -0.012, -0.003,  0.025, -0.011, -0.   ,  0.019,  0.017, -0.018,  0.001,
    0.05 ,  0.007, -0.089, -0.058,  0.017,  0.063,  0.022, -0.04 ,  0.027, -0.027,
   -0.026,  0.046, -0.059, -0.069,  0.11 ,  0.002,  0.003, -0.004]),
      0.761547267437,  # corner penalty
      2.27284860611   # occlusion penalty
)

params296 = py_indoor_context.ManhattanHyperParameters(
    np.array(
  [ -4.931e-02,  -6.714e-02,   1.676e-01,  -6.338e-02,   8.373e-02,  -2.919e-02,
     1.316e-01,  -1.015e-01,  -4.313e-02,  -1.087e-02,   1.291e-02,  -2.647e-03,
    -8.929e-03,   1.288e-02,  -5.762e-03,   2.517e-01,  -1.430e-01,  -1.564e-01,
     7.260e-02,  -4.484e-02,  -4.384e-02,   4.575e-02,  -4.353e-02,  -3.287e-03,
    -2.298e-02,  -1.398e-04,   3.320e-02,  -7.684e-02,   1.205e-02,   9.294e-02,
    -3.204e-02,  -1.183e-02,   6.301e-02,   3.088e-02,  -3.338e-02,   3.699e-03,
    -1.496e-02,   6.620e-02,  -9.153e-02,  -7.162e-02,   7.904e-02,  -1.154e-02,
     1.117e-02,   2.903e-02,  -5.777e-02,   1.816e-02,   7.089e-03,  -3.622e-02,
     8.772e-03,  -1.825e-02,   1.362e-02,  -4.473e-02,   5.501e-02,  -1.602e-02,
     5.593e-02,  -4.784e-02,  -1.481e-02,  -2.842e-02,  -4.968e-02,   1.270e-01,
    -6.721e-02,   6.476e-02,   3.518e-03,  -6.176e-02,   1.877e-02,   6.160e-02,
    -6.331e-02,   1.231e-01,  -8.619e-02,   1.634e-02,  -1.280e-02,  -5.805e-03,
     4.013e-02,  -2.206e-02,  -2.885e-02,  -5.342e-03,  -1.406e-02,   3.048e-02,
     3.342e-03,  -8.570e-03,   4.479e-03,  -1.488e-03,  -1.621e-02,   2.822e-02,
    -1.817e-02,   5.182e-02,  -5.327e-02,  -4.063e-02,   5.091e-03,   5.441e-02,
     3.628e-02,  -1.680e-02,  -3.056e-02,   7.469e-03,   1.245e-02,  -3.101e-02,
    -2.438e-02,   1.783e-03,   3.383e-02,  -4.592e-02,  -5.554e-02,   8.698e-02,
     5.673e-02,  -3.368e-03,  -4.595e-02,  -2.972e-02,   2.965e-02,   6.109e-05]),
  0.246750742197,
  2.14338445663)

params302 = py_indoor_context.ManhattanHyperParameters(
    np.array(
  [-0.065, -0.04 ,  0.151, -0.077,  0.093, -0.022,  0.172, -0.165, -0.01,  -0.013,
    0.014, -0.001,  0.008, -0.001, -0.01 ,  0.248, -0.127, -0.174,  0.057, -0.024,
   -0.052,  0.045, -0.042, -0.005, -0.019, -0.002,  0.03 , -0.073,  0.014,  0.083,
   -0.03 , -0.015,  0.064,  0.024, -0.027,  0.005, -0.012,  0.059, -0.084, -0.073,
    0.073,  0.   ,  0.015,  0.033, -0.07 ,  0.031,  0.011, -0.06 ,  0.013, -0.025,
    0.017, -0.041,  0.04 ,  0.001,  0.067, -0.06 , -0.011, -0.072, -0.022,  0.153,
   -0.072,  0.083, -0.017, -0.047,  0.009,  0.055, -0.064,  0.116, -0.075,  0.007,
   -0.016,  0.015,  0.038, -0.03 , -0.014, -0.004, -0.001,  0.007, -0.007,  0.001,
    0.005, -0.004, -0.016,  0.032, -0.014,  0.058, -0.07 , -0.027, -0.003,  0.046,
    0.03 , -0.01 , -0.032,  0.007,  0.004, -0.018, -0.025,  0.006,  0.028, -0.05,  -0.04,
    0.078,  0.041,  0.011, -0.045, -0.021,  0.006,  0.013,]),
  0.437838941813,
  2.4081454277)

params353 = py_indoor_context.ManhattanHyperParameters(
    np.array(
  [ -5.622e-02, -4.972e-02,  1.525e-01, -8.242e-03,  4.615e-02, -5.438e-02,
     7.366e-02, -6.751e-02, -8.810e-03,  4.823e-03, -1.580e-03, -4.194e-03,
    -1.474e-02,  1.416e-02,  8.450e-04,  2.445e-01, -1.175e-01, -1.828e-01,
     3.165e-02, -8.135e-05, -4.985e-02,  3.608e-02, -3.749e-02,  2.089e-03,
    -2.624e-02,  1.397e-02,  1.762e-02, -8.967e-02,  6.889e-02,  2.981e-02,
    -3.956e-02,  1.252e-02,  3.884e-02,  4.672e-03, -1.375e-02,  1.340e-02,
    -4.623e-02,  4.227e-02,  7.071e-03, -5.666e-02,  2.354e-02,  5.149e-02,
     1.377e-02, -8.075e-04, -1.863e-02, -1.195e-02,  3.373e-02, -3.124e-02,
     4.107e-03, -1.465e-02,  1.515e-02, -6.374e-02,  2.309e-02,  6.334e-02,
     3.880e-03,  4.527e-02, -8.997e-02,  7.842e-03, -3.783e-02,  4.877e-02,
    -2.072e-03, -1.919e-02,  3.059e-02, -6.326e-02,  2.942e-02,  4.848e-02,
    -9.391e-04,  7.149e-02, -1.016e-01,  4.737e-02, -3.657e-02, -1.772e-02,
    -1.819e-02,  2.434e-02, -9.818e-03, -2.337e-02,  1.497e-02,  1.320e-02,
     5.028e-02, -1.415e-02, -3.096e-02, -3.562e-02,  1.428e-02,  3.404e-02,
     3.842e-02, -3.548e-03, -5.521e-02, -7.373e-03, -9.877e-03,  2.641e-02,
     9.077e-03,  9.827e-03, -2.966e-02,  3.372e-02, -8.624e-03, -3.907e-02,
    -1.446e-02, -1.437e-02,  4.316e-02,  3.110e-03, -4.835e-02,  3.878e-02,
    -8.078e-02,  3.293e-02,  4.119e-02,  2.011e-02, -1.354e-02, -6.045e-03]),
  0.501899659634,
  1.86498379707)

params472 = py_indoor_context.ManhattanHyperParameters(
    np.array(
 [ -3.438e-02, -4.327e-02,  1.118e-01, -2.347e-02,  6.145e-02, -5.448e-02
,   4.260e-02, -1.836e-02, -3.474e-02,  4.045e-03, -3.746e-03, -3.863e-04
,  -2.165e-02,  2.154e-02,  1.636e-04,  2.324e-01, -1.165e-01, -1.668e-01
,  -4.584e-03,  1.770e-02, -2.071e-02, -1.393e-02,  2.612e-03,  1.669e-02
,  -3.035e-02,  1.580e-02,  2.090e-02, -8.017e-02,  4.724e-02,  4.724e-02
,  -3.390e-02,  5.859e-03,  4.026e-02, -5.004e-03, -5.589e-03,  1.564e-02
,   2.460e-02, -6.690e-03, -3.200e-02, -2.310e-02,  9.397e-03,  2.131e-02
,   1.488e-02, -3.216e-03, -1.676e-02,  1.854e-02, -8.400e-03, -1.455e-02
,   9.655e-03, -2.384e-02,  2.040e-02,  2.878e-03, -1.501e-02,  1.890e-02
,   2.386e-02,  4.070e-04, -4.442e-02, -1.564e-03, -1.789e-02,  3.163e-02
,  -3.844e-02, -7.613e-04,  5.640e-02, -3.415e-02, -1.290e-02,  6.740e-02
,   6.380e-03,  8.334e-02, -1.292e-01, -1.796e-02,  1.175e-02,  1.018e-02
,  -4.128e-03,  2.545e-02, -3.404e-02, -2.726e-02,  3.657e-02, -1.463e-02
,  -6.319e-03,  2.722e-03,  3.081e-03, -1.076e-02, -2.676e-03,  2.141e-02
,   1.499e-02, -1.389e-04, -2.351e-02, -1.585e-02, -6.468e-03,  3.416e-02
,   2.047e-02, -5.986e-03, -2.271e-02,  2.524e-02, -1.650e-02, -1.361e-02
,  -1.098e-02, -1.484e-02,  3.865e-02, -8.940e-03, -3.591e-02,  3.845e-02
,  -2.280e-02,  1.825e-03,  1.807e-02,  7.893e-03,  7.195e-03, -1.389e-02]),
      0.518578052521,
      2.80577945709)

params489 = py_indoor_context.ManhattanHyperParameters(
    np.array(
  [-0.051 ,-0.023 , 0.106 ,-0.009 , 0.059 ,-0.072 , 0.056 ,-0.022 ,-0.049 ,-0.002,
    0.001 , 0.002 ,-0.018 , 0.02  ,-0.003 , 0.238 ,-0.116 ,-0.175 ,-0.003 , 0.009,
   -0.01  ,-0.02  , 0.007 , 0.018 ,-0.035 , 0.018 , 0.024 ,-0.086 , 0.041 , 0.065,
   -0.032 , 0.004 , 0.041 ,-0.001 ,-0.011 , 0.018 , 0.029 ,-0.012 ,-0.03  ,-0.034,
    0.031 , 0.004 , 0.005 , 0.009 ,-0.021 , 0.015 ,-0.005 ,-0.015 , 0.016 ,-0.026,
    0.015 , 0.016 ,-0.039 , 0.037 , 0.01  , 0.006 ,-0.029 , 0.002 ,-0.01  , 0.013,
   -0.058 , 0.005 , 0.076 ,-0.03  ,-0.014 , 0.064 , 0.017 , 0.081 ,-0.141 ,-0.015,
   -0.015 , 0.05  , 0.003 , 0.029 ,-0.052 ,-0.021 , 0.034 ,-0.019 , 0.004 , 0.013,
   -0.015 , 0.008 ,-0.015 , 0.011 ,-0.008 , 0.004 , 0.006 ,-0.015 ,-0.004 , 0.03,
    0.043 ,-0.03  ,-0.02  , 0.006 , 0.007 ,-0.019 ,-0.014 ,-0.014 , 0.042 ,-0.008,
   -0.028 , 0.031 ,-0.03  ,-0.015 , 0.038 , 0.007 ,-0.002 ,-0.005]),
  0.488729357719,
  2.48595023155)

params537 = py_indoor_context.ManhattanHyperParameters(
    np.array(
  [ -3.477e-02, -5.049e-02,  1.227e-01, -3.346e-02,  8.044e-02, -6.738e-02
,   6.110e-02, -5.127e-02, -1.409e-02,  2.795e-03, -8.375e-04, -2.532e-03
,  -2.392e-02,  2.136e-02,  3.726e-03,  2.625e-01, -1.333e-01, -1.860e-01
,  -1.202e-02,  2.162e-02, -1.516e-02, -3.665e-03, -1.618e-02,  2.928e-02
,  -1.962e-02,  4.530e-03,  2.167e-02, -6.100e-02,  3.884e-02,  3.179e-02
,  -2.450e-02,  1.874e-03,  3.249e-02, -6.905e-03, -1.056e-02,  2.579e-02
,   4.017e-03,  4.930e-03, -1.598e-02, -4.945e-03, -1.114e-02,  2.501e-02
,   1.381e-02,  6.257e-03, -2.884e-02,  1.088e-02,  4.881e-03, -2.260e-02
,   1.389e-02, -2.208e-02,  1.177e-02, -1.593e-02, -8.707e-03,  3.838e-02
,   6.292e-03,  1.416e-02, -3.743e-02, -3.696e-02,  1.492e-02,  3.583e-02
,  -6.232e-02,  3.141e-02,  4.446e-02, -2.979e-02,  2.908e-03,  3.851e-02
,  -5.385e-02,  1.002e-01, -6.673e-02,  4.682e-02, -2.694e-02, -3.261e-02
,   4.513e-03,  1.686e-02, -3.413e-02, -1.932e-02, -2.561e-03,  3.439e-02
,  -5.403e-03,  1.293e-02, -6.451e-03, -4.793e-03, -5.319e-03,  1.612e-02
,  -6.570e-03,  1.015e-02, -5.671e-03, -3.792e-03, -8.037e-03,  1.811e-02
,   2.082e-02, -5.160e-05, -3.259e-02,  1.482e-02,  1.439e-02, -4.547e-02
,  -5.841e-03, -1.309e-02,  2.834e-02, -1.703e-02, -4.643e-02,  5.440e-02
,  -1.037e-02, -7.967e-03,  1.579e-02,  8.033e-03,  4.465e-03, -1.150e-02]),
  0.571090400219,
  2.38941001892)

params = params537

output_path = 'experiments/mar05_depthloss_mono-all_large_iter537man'

# Check that we're not about to overwrite a previous experiment
if os.path.exists(output_path):
    print 'Error: Experiment dir already exists: ',output_path
    exit(-1)

# Load the dataset
mgr = py_indoor_context.TrainingManager()
instances = []
for sequence,frame_ids in test_set:
    print '  From %s loading frames %s' % (sequence, ','.join(map(str, frame_ids)))
    instances += training_helpers.load_sequence(mgr, sequence, frame_ids)

print 'Loaded %d instances' % len(instances)

# Compute features
fm = py_indoor_context.FeatureManager(feature_store)
#for inst in instances:
#    fm.ComputeMonoFeatures(inst, 'all,-nbr_sweeps')
#    fm.CommitFeatures()
#mgr.NormalizeFeatures(fm)

# Run evaluation
reporter = training_helpers.Reporter([], instances, fm, output_path)
reporter.generate_report(params, extended=False)
